package com.zero.base;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.EntityPathBase;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 所有查询类的基类
 * https://www.jianshu.com/p/dbdf04070243
 */
@NoArgsConstructor
@AllArgsConstructor
@Component
public abstract class BaseQuery {
    @ApiModelProperty("第几页")
    @Setter
    @Getter
    private int page = 1;

    @ApiModelProperty("显示多少条")
    @Setter
    @Getter
    private int pageSize = 10;

    @ApiModelProperty(
            value = "排序",
            example = "[\"createTime,desc\",\"zero\"]"
    )
    @Setter
    protected List<String> sortList;

    /**
     * 这儿要考虑默认的（后端固定排序）和前端动态排序
     *
     * @return
     */
    private List<String> getSortList() {
        List<String> strings = this.initSort();
        if (!CollectionUtils.isEmpty(strings)) {
            if (!CollectionUtils.isEmpty(sortList)) {
                strings.addAll(sortList);
            }
            return strings;
        }
        return sortList;
    }

    /**
     * 定义一个初始化排序方法
     *
     * @return
     */
    protected List<String> initSort() {
        return null;
    }

    /**
     * 初始条件
     *
     * @return
     */
    protected Predicate initPredicate(EntityPathBase qe) {
        return qe.isNotNull().or(qe.isNull());
    }

    /**
     * 条件获取
     */
    public Predicate predicate() {
        return null;
    }

    /**
     * 分页获取
     *
     * @return
     */
    @JsonIgnore
    Pageable getPageable() {
        return PageRequest.of(page - 1, pageSize, getSort());
    }

    /**
     * 排序获取
     *
     * @return
     */
    @JsonIgnore
    Sort getSort() {
        //设置后台默认排序
        Sort sort = Sort.unsorted();
        List<Sort.Order> orderList = new ArrayList<>();
        List<String> sorts = getSortList();
        if (!CollectionUtils.isEmpty(sorts)) {
            for (String string : sorts) {
                if (StringUtils.isNotBlank(string)) {
                    String[] split = string.split(",");
                    String field = split[0];
                    Sort.Direction direction = Sort.Direction.ASC;
                    if (split.length > 1) {
                        String s = split[1];
                        if (Sort.Direction.DESC.toString().equalsIgnoreCase(s)) {
                            direction = Sort.Direction.DESC;
                        }
                    }
                    orderList.add(new Sort.Order(direction, field));
                }
            }
        }
        if (!CollectionUtils.isEmpty(orderList)) {
            sort = Sort.by(orderList);
        }
        return sort;
    }
}

